package com.example.admin.yuejian21.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.admin.yuejian21.Bean.MasterDetail;
import com.example.admin.yuejian21.Model.MasterModel;
import com.example.admin.yuejian21.R;
import com.example.admin.yuejian21.fragment.CourseFragment;
import com.example.admin.yuejian21.fragment.ProblemFragment;

import it.sephiroth.android.library.picasso.Picasso;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;



public class MasterDetailActivity extends FragmentActivity implements View.OnClickListener {
    private TextView Course;
    private TextView Problem;

    private Fragment Tab01;
    private Fragment Tab02;
    private ImageView dashiPhoto,btn_back;
    private TextView train_name,dashi_jianjie;
    private String dashi_id,address;
    private MasterDetail masterDetail;
    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_master_detail);
        initView();
        initEvent();
        setSelect(2);
        initData();}
    private void initEvent(){
      Course.setOnClickListener(this);
    Problem.setOnClickListener(this);

        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initView(){
     Course =(TextView) findViewById(R.id.dashi_kt);
    Problem = (TextView)findViewById(R.id.dashi_wt);
        train_name = (TextView) findViewById(R.id.train_name);
        dashi_jianjie = (TextView) findViewById(R.id.dashi_jianjie);
        dashiPhoto = (ImageView) findViewById(R.id.dashiPhoto);
        btn_back = (ImageView) findViewById(R.id.train_back);
        mContext=MasterDetailActivity.this;
    }
    private void initData(){
        Intent intent = getIntent();
        dashi_id = intent.getStringExtra("dashi_id");
        showData();
    }
    private void showData(){
        MasterModel masterModel = new MasterModel();
        Call<MasterDetail> call = masterModel.getMasterDetail(dashi_id);
        call.enqueue(new Callback<MasterDetail>() {
            @Override
            public void onResponse(Call<MasterDetail> call, Response<MasterDetail> response) {
                masterDetail = response.body();
                train_name.setText(masterDetail.getRealname());
                dashi_jianjie.setText(masterDetail.getIntroduction());
                String address = "http://172.24.10.175/workout/Uploads/"+masterDetail.getThumb();
                if(!masterDetail.getThumb().equals("")){
                    Picasso.with(mContext).load(address).into(dashiPhoto);
                }
            }

            @Override
            public void onFailure(Call<MasterDetail> call, Throwable t) {

            }
        });
    }

    private void setSelect(int i){
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i)
        {
            case 0:
                if (Tab01==null)
                {
                    Tab01 = new CourseFragment();
                    transaction.add(R.id.dashi_frame, Tab01);
                }
                else{
                    transaction.show(Tab01);
                }
                break;
            case 1:
                if (Tab02==null)
                {
                    Tab02 = new ProblemFragment();
                    transaction.add(R.id.dashi_frame,Tab02);
                }else {
                    transaction.show(Tab02);
                }
                break;
            default:
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (Tab01 != null)
        {
            transaction.hide(Tab01);
        }
        if (Tab02 != null)
        {
            transaction.hide(Tab02);
        }
    }

    //@Override
    public void onClick(View view) {
        resetImgs();
        switch (view.getId())
        {
            case R.id.dashi_kt:
                setSelect(0);
                break;
            case R.id.dashi_wt:
                setSelect(1);
                break;
            default:
                break;
        }

    }

    private void resetImgs(){
    }
}
